<?php
function windowsToUnixPathC ($str) {
	$str = preg_replace('/[a-zA-Z]:\\\/', '\\', $str);
	$str = str_replace("\\",'/', $str);
	return $str;
}
$base_pages = array("install.php");
$base_url[0] = windowsToUnixPathC(realpath(getcwd().'/')).'/';
foreach($base_pages as $key => $value) {
	$base_url = explode($value, $base_url[0]);
}
$baseurl = $base_url[0];
$visual[0] = $_SERVER["REQUEST_URI"];
foreach($base_pages as $key => $value) {
	$visual = explode($value, $visual[0]);
}
$visual = $visual[0];
			?><?php require($baseurl.'includes/core.standard.vars.php'); ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!-- design by alec gorge -->
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Installer for LemurCMS</title>
<style type="text/css">
@import 'includes/install/installer.css';
</style>
<script type="text/javascript" src="includes/admin/js/jquery-1.3.1.min.js"></script>
<script type="text/javascript" src="includes/admin/js/jquery.alphanumeric.js"></script>
<script type="text/javascript">
$(document).ready(function() {
	$('#user').alphanumeric();
	$('#dbuse').change(function () {
		var class = $(this).val();
		$('.init-hide').hide()
		$('.'+class).show()
	});
		var class = $('#dbuse').val();
		$('.init-hide').hide()
		$('.'+class).show()
});
</script>
</head>

<body>
<div id="wrapper">
	<div id="head">
		<h1>Installer <span>for LemurCMS</span></h1>
	</div>
	<div id="content-sidebar-wrapper">
		<div id="content">
			<div class="gutter">
			<h2>Step One: System Requirements</h3>
			<h3>Your server needs to meet certain requirements for LemurCMS run.</h3>
			<ul>
				<li>You must have PHP version 5.1.</li>
				<li>PHP GD image library.</li>
				<li>PHP MySQL 5 OR PDO with SQLite 3 support.</li>
				<li>An Apache server</li>
				<li>The following directories must be writeable (chmod 0777 or 0775)
					<ul>
						<li>temp/</li>
						<li>uploaded-files/</li>
						<li>uploaded-files/_thumbs/</li>
					</ul>
				</li>
			</ul>
			<h3>It is recommended that you have the following:</h3>
			<ul>
				<li>An Apache server with mod_rewrite enabled.</li>			
			</ul>
			<h2>Testing Requirements...</h2>
			<?php
				$o = 1;
				$sqlite = false;
				if(get_magic_quotes_gpc()) {
					echo '<div class="error">You have Magic Quotes enabled. Disable them.</div>';
					$o = 0;
				}
				if(!is_writable($baseurl.'temp/'))
					$nonWrite[] = 'temp/';
				if(!is_writable($baseurl.'config.php'))
					$nonWrite[] = 'temp/';
				if(!is_writable($baseurl.'.htaccess'))
					$nonWrite[] = '.htaccess';
				if(!is_writable($baseurl.'uploaded-files/'))
					$nonWrite[] = 'uploaded-files/';
				if(!is_writable($baseurl.'uploaded-files/_thumbs/'))
					$nonWrite[] = 'uploaded-files/_thumbs/';
				if(is_array($nonWrite)) {
					$o = 0;
					foreach($nonWrite as $value) {
						echo "<div class='error'>$value isn't writeable. Please chmod it to 0755 (better security) or 0777 (use if 0775 doesn't work) for installation to continue.</div>";
					}
				}
				if(phpversion() >= 5.2) {
					echo '<div class="success">You have PHP version '.phpversion().' which is high enough to run LemurCMS.</div>';
					if(class_exists('PDO')) {
						try {
							$dbh = new PDO('sqlite::memory:');
							$sqlite = true;
						}
						catch (PDOException $e) {
							$sqlite = false;
						}
					}
					else {
						echo '<div class="error">You don\'t have PDO support. If you don\'t have MySQL you will need to install this.</div>';
					}
				}
				else {
					echo '<div class="error">You have PHP version '.phpversion().' which isn\'t high enough to run LemurCMS. Installation cannot continue.</div>';
					$o = 0;
				}
				if(function_exists("gd_info")) {
					$x = gd_info();
					$x = $x['GD Version'];
					$x = explode('bundled (', $x);
					$x = explode('compatible)', $x[1]);
					echo '<div class="success">You have the PHP GD image library version '.$x[0].' which is sufficent to run LemurCMS.</div>';
				}
				else {
					echo '<div class="error">You don\'t have the PHP GD image library which isn\'t sufficent to run LemurCMS. Installation cannot continue.</div>';
					$o = 0;
				}
				if(function_exists("mysql_query")) {
					$mysql = true;
					echo '<div class="success">You have PHP MySQL support which is required for LemurCMS. You many not have MySQL version 5 so fill out the form below.</div>';
				}
				else {
					$mysql = false;
					echo '<div class="error">You don\'t have PHP MySQL support which is required for LemurCMS. Installation cannot continue.</div>';
					$o = 0;
				}
				if($mysql == false && $sqlite == false) {
					$o = 0;
					echo '<div class="error">You don\'t have PHP MySQL support or PHP PDO SQLite support. One of which is required for LemurCMS. Installation cannot continue.</div>';
					echo '<div class="error">You need to uncomment the line that says "extension=php_pdo_sqlite.dll" in your php.ini file for PHP PDO SQLite. It is usally near line 650. If you are using XAMPP edit the file at /xampp/apache/bin/php.ini</div>';
				}
				if($mysql == true && $sqlite == true) {
					echo '<div class="success">You have PDO SQLite support and PHP MySQL support. You can choose both in the installer.</div>';
				}
				if($o == 1) {
					if($_POST) {
						if($_POST['dbuse'] == 'mysql') {
							$mysqlUse = true;
							$dbname = $_POST['dbname'];
							$dbuser = $_POST['dbuser'];
							$dbpass = $_POST['dbpass'];
							$dbhost = $_POST['dbhost'];
							$dbport = $_POST['dbport'];
							$dbtable = $_POST['dbpre'];
						}
						elseif($_POST['dbuse'] == 'sqlite') {
							$sqliteUse = true;
							$dbtable = $_POST['sqlitedbpre'];
						}
						$user = $_POST['user'];
						$pass = $_POST['pass'];
						$cpass = $_POST['cpass'];
						$email = $_POST['email'];
						$sitetitle = $_POST['sitetitle'];
						$siteauthor = $_POST['siteauthor'];
						$slogan = $_POST['slogan'];
						$metadesc = $_POST['metadesc'];
						$metakey = $_POST['metakey'];
						$cleanurls = $_POST['cleanurls'];
						$footer = $_POST['footer'];
						if($pass !== $cpass) {
							$err = 1;
							echo '<div class="error">Your passwords aren\'t the same.</div>';
						}
						else {
							if($sqliteUse) {
								if(function_exists('chmod'))
									chmod($baseurl.'includes/db/', 0777);
								try {
									$dbh = new PDO('sqlite:'.$baseurl.'includes/db/lemurcms.sqlite3');
								}
								catch (PDOException $e) {
									echo '<div class="error">Couldn\'t create SQLite Database</div>';
								}
								ob_start();
									eval('?>'.file_get_contents($baseurl.'includes/install/install-SQLite.sql'));
									$output = ob_get_contents();
								ob_end_clean();
								$queries =explode('--', $output);
								include($baseurl.'includes/db/sqlite.core.class.php');
								$db = new DB;
								$db->database = $baseurl.'includes/db/lemurcms.sqlite3';
								$connect = $db->open();
								if(!$connect)
									die('DB Connection Failed.');
								foreach($queries as $value) {
									$db->query(trim($value)) or die('<div class="error">'.$db->error().'</div>');
								}
							}
							else {
								ob_start();
									eval('?>'.file_get_contents($baseurl.'includes/install/install-MySQL.sql'));
									$output = ob_get_contents();
								ob_end_clean();
								$queries =explode('--', $output);
								include($baseurl.'includes/db/mysql.core.class.php');
								$db = new DB;
								$db->host = $dbhost.":".$dbport;
								$db->user = $dbuser;
								$db->password = $dbpass;
								$db->database = $dbname;
								$db->persistent = true;
								$connect = $db->open();
								if(!$connect)
									die('DB Connection Failed.');
								foreach($queries as $value) {
									$db->query($value) or die('<div class="error">'.$db->error().'</div>');
								}
							}
							/*
							- write htaccess,
							- write config
							*/
							file_put_contents($baseurl.'config.php', 
								sprintf(CONFIG_FORMAT, $dbhost,$dbuser,$dbpass,$dbname,$dbtable,$dbport,$_POST['dbuse'], ($_POST['cleanurls'] =='Enabled' ? true : false))
							);
							if($cleanurls == 'Enabled') {
								$output = file_get_contents($baseurl.'.htaccess').HTACCESS_FORMAT.sprintf(MOD_REWRITE_ENABLED_FORMAT, $visual, $visual, $visual, $visual);
								file_put_contents($baseurl.'.htaccess', $output);
							}
							else {
								$output = file_get_contents($baseurl.'.htaccess').HTACCESS_FORMAT.sprintf(MOD_REWRITE_DISABLED_FORMAT, $visual, $visual, $visual, $visual);
								file_put_contents($baseurl.'.htaccess', $output);
							}
							echo '<div class="success">LemurCMS was successfully installed. View your site <a href="index.php?frominstaller=true">here</a> and go to the administration <a href="admin.php">here</a></div>';
							exit();
						}
					}
			?>
			<h2>Step Two: Database Info</h2>
			<form action="#" method="post">
			<div class="form-row"><label for="dbuse">What kind of Database to use?</label>
				<select name="dbuse" id="dbuse">
					<?php if($mysql == true) { ?>
						<option value="mysql"<?php if($_POST['dbuse'] == 'mysql') { ?> selected="selected"<?php } ?>>MySQL</option>
					<?php } ?>
					<?php if($mysql == true) { ?>
						<option value="sqlite"<?php if($_POST['dbuse'] == 'sqlite') { ?> selected="selected"<?php } ?>>SQLite</option>
					<?php } ?>
				</select>
				<p class="small gray">MySQL is better for larger, more popular sites, where SQLite will allow you to easily transfer your site between servers because SQLite is a single file.</p>
			</div>
			<div class="mysql init-hide<?php if($mysql == true) { ?> display<?php } ?><?php if($_POST['dbuse'] == 'mysql') { ?> display"<?php } ?>">
				<div class="form-row"><label for="dbname">Database Name</label><input type="text" name="dbname" value="<?php echo $_POST['dbname'] ?>" /></div>
				<div class="form-row"><label for="dbuser">Database Username</label><input type="text" name="dbuser" value="<?php echo $_POST['dbuser'] ?>" /></div>
				<div class="form-row"><label for="dbpass">Database Password</label><input type="password" name="dbpass" value="<?php echo $_POST['dbpass'] ?>" /></div>
				<div class="form-row"><label for="dbhost">Database Host (usually localhost)</label><input type="text" name="dbhost" value="<?php if($_POST){echo $_POST['dbhost'];}else{echo'localhost';} ?>"/></div>
				<div class="form-row"><label for="dbport">Database Port (usally 3306)</label><input type="text" name="dbport" value="<?php if($_POST){echo $_POST['dbport'];}else{echo'3306';} ?>" /></div>
				<div class="form-row"><label for="dbpre">Database Table Prefix</label><input type="test" name="dbpre" value="<?php if($_POST){echo $_POST['dbpre'];}else{echo'';} ?>" /><p class="small gray">(What will come before each table ex. "cms_")</p></div>
			</div>
			<div class="sqlite init-hide<?php if($mysql == false && $sqlite == true) { ?> display<?php } ?><?php if($_POST['dbuse'] == 'sqlite') { ?> display"<?php } ?>">
				<div class="form-row"><label for="sqlitedbpre">Database Table Prefix</label><input type="test" name="sqlitedbpre" value="<?php if($_POST){echo $_POST['sqlitedbpre'];}else{echo'';} ?>" /><p class="small gray">(What will come before each table ex. "cms_")</p></div>
			</div>
			<h2>Step Three: Create the first user</h2>
			<div class="form-row"><label for="user">Username</label><input type="text" name="user" id='user' value="<?php echo $_POST['user'] ?>" /></div>
			<div class="form-row"><label for="pass">Password</label><input type="password" name="pass" id='pass' value="<?php echo $_POST['pass'] ?>" /></div>			
			<div class="form-row"><label for="cpass">Confirm Password</label><input type="password" name="cpass" id='cpass' value="<?php echo $_POST['cpass'] ?>" /></div>
			<div class="form-row"><label for="email">Email</label><input type="text" name="email" value="<?php echo $_POST['email'] ?>" /></div>			
			<h2>Step Four: Site Information</h2>
			<div class="form-row"><label for="sitetitle">Site Title (Example: Example Online)</label><input type="text" name="sitetitle" value="<?php echo $_POST['sitetitle'] ?>" /></div>			
			<div class="form-row"><label for="siteauthor">Site Author (Example: John Doe)</label><input type="text" name="siteauthor" value="<?php echo $_POST['siteauthor'] ?>" /></div>			
			<div class="form-row"><label for="slogan">Site Slogan (Example: We strive...)</label><input type="text" name="slogan" value="<?php echo $_POST['slogan'] ?>" /></div>			
			<div class="form-row"><label for="metadesc">Site Meta Description</label><input type="text" name="metadesc" value="<?php echo $_POST['metadesc'] ?>" /><p class="small gray">Write a keyword-laden sentence that describes what your site is about and your goals with the site.</p></div>			
			<div class="form-row"><label for="metakey">Site Meta Keywords</label><input type="text" name="metakey" value="<?php echo $_POST['metakey'] ?>" /><p class="small gray">Choose about 20-30 words that describe your site.</p></div>			
			<div class="form-row"><label for="footer">Footer Text</label><input type="text" name="footer" value="<?php echo $_POST['footer'] ?>" /><p class="small gray">This will be the footer for your site.</p></div>			
			<div class="form-row"><label for="cleanurls">Clean Urls</label><select name='cleanurls'><option>Enabled</option><option>Disabled</option></select><p class="small gray">Please only enable this if you are positive that your server supports mod_rewrite.</p></div>			
			<div class="form-row-last"><input type="submit" value="Submit" name="submit"/></div>
			</form>
			<?php
			}
			?>
			</div>			
		</div>
		<div id="footer">
			<p>design by <a href="http://ramblingwood.com">Ramblingwood</a></p>
		</div>
	</div>
</div>
</body>

</html>





<!-- design by alec gorge -->